import os
import copy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import colorcet as cc
import warnings
warnings.filterwarnings("ignore")

sns.set_style('darkgrid')
sns.set(rc={'figure.figsize':(14,8)})

# dataset_names = ["WALMART_SALES_DATA.csv"]
# data_path = "../mydatase/market/WALMART_SALES_DATA.csv"
#
# # 操作文件
# df = pd.read_csv(data_path)
# r_sum = df.shape[0]
# file_count, file_long = r_sum // 143, r_sum % 143  # 文件个数，最后一个文件的行数
#
# for i in range(0, file_count - 1):
#     d = df.iloc[i * 143:i * 143 + 143, :]
#     d.to_csv('../mydatase/market/'+str(i + 1) + '.csv', index=False)
#
# d = df.iloc[len(df) - file_long:len(df), :]  # 获取最后一个文件的行索引
# d.to_csv('../mydatase/market/'+str(i + 2) + '.csv', index=False)  # 文件保存

dataset_names = [str(index+1)+'.csv' for index in range(45) ]

data_path = "../mydatase/market"
datasets = [os.path.join(data_path, data_name) for data_name in dataset_names]

print(datasets)

dfs = dict()
for data_name, data_path in zip(dataset_names, datasets):
    dfs[data_name] = pd.read_csv(data_path)
    # dfs[data_name].drop(columns=['Store'],axis=0)

# example:
# stort_1 = dfs["stort_1.csv"]
# stort_2 = dfs["stort_2.csv"]

# data = '05-02-2010'
# print(data[6:]+'-'+data[3:5]+'-'+data[0:2])
train_dataset = []
val_data = []
for i in range(len(dataset_names)):
    file_name = dfs[dataset_names[i]]
    file_name = file_name.drop(columns = ['Store'])
    # 日期处理
    for j in range (file_name.shape[0]):
        tmp_data = file_name.iloc[j,0][6:]+'-'+file_name.iloc[j,0][3:5]+'-'+file_name.iloc[j,0][0:2]
        file_name.iloc[j,0] = tmp_data
        # tmp_merger_data = str(file_name.iloc[j,0]) + ',' + str(file_name.iloc[j,2]) + ',' + str(file_name.iloc[j,3]) \
        #                   + ',' + str(file_name.iloc[j,4]) + ',' + str(file_name.iloc[j,5]) + ',' + str(file_name.iloc[j,6])
        # file_name.iloc[j, 7] = tmp_merger_data
    # tmp_data = str(file_name["Date"])[6:]+'-'+str(file_name["Date"])[3:5]+'-'+str(file_name["Date"])[0:2]
    # print(tmp_data)
    file_name["Date"] = pd.to_datetime(file_name["Date"], format="%Y-%m-%d")
    # print (f" {min(file_name['Date'])}, {max(file_name['Date'])}")
    # file_name.set_index("Date", inplace=True)
    # file_name["District"] = 'Stort' + str(i + 1) ;
    file_name['text'] = file_name.apply(lambda row: str(row['Date'])+','+str(row['Holiday_Flag'])+','+str(row['Temperature'])+','+
                                         str(row['Fuel_Price'])+','+str(row['CPI'])+','+str(row['Unemployment']) ,  axis=1)
    # df['C'] = df.apply(lambda row: row['A'] + row['B'], axis=1)
    train_dataset.append(file_name.iloc[:122]);# 暂时只存前面的116条数据
    val_data.append(file_name.iloc[122:]);# 暂时只存前面的116条数据
    # dfs[dataset_names[i]] = file_name # 替换修改内容（缓存）
    # file_name.iloc[:116].to_csv('../mydatase/marketData3/Stort' + str(i + 1) + '.csv', index=True)
    # file_name.iloc[116:].to_csv('../mydatase/marketData3/Stort' + str(i + 1) + '_test.csv', index=True)

pd.concat(train_dataset).to_csv('../mydatase/marketData3/train.csv', index=True)
pd.concat(val_data).to_csv('../mydatase/marketData3/test.csv', index=True)

#画出相关的图案出来
# def cor_matrix(df):
#     plt.figure(figsize=(18,8))
#     sns.heatmap(df.corr(),annot=True,cmap='Greens',linewidths=0.2)
#     plt.show()
#     plt.close()
# cor_matrix(dfs["1.csv"])

# for i in range(len(dfs)):
#     Weekly_Sales = dfs[str(i+1)+'.csv']["Weekly_Sales"]
#     plt.ticklabel_format(style='plain')
#     ax = Weekly_Sales.plot()
#     ax.set_xlabel("Date")
#     ax.set_title("Weekly_Sales")
#     plt.show()



